/**
 * Copyright (C) 2013-2014 the original author or authors.
 */
package com.foreveross.bsl.mdm.ios.application.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.inject.Named;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;

import com.foreveross.bsl.common.utils.bean.BeanHelper;
import com.foreveross.bsl.common.utils.enums.OperatEnum;
import com.foreveross.bsl.mdm.ios.application.MdmEnrollApplication;
import com.foreveross.bsl.mdm.ios.application.vo.MdmEnrollmentVo;
import com.foreveross.bsl.mdm.model.ios.domain.MdmEnrollment;

/**
 * @description
 *
 * @author heyaowen
 * @version v1.0
 *
 * @2014-7-18
 *
 */
@Named("iosMdmEnrollApplication")
public class MdmEnrollApplicationImpl implements MdmEnrollApplication {

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.foreveross.bsl.system.application.DatabaseInfoApplication#saveEntity
	 * (com.foreveross.bsl.system.application.vo.DatabaseInfoVO)
	 */
	@Override
	public MdmEnrollmentVo saveEntity(MdmEnrollmentVo entity) {
		// 转换成领域对象
		MdmEnrollment mdmEnrollment = new MdmEnrollment();
		BeanHelper.copyProperties(mdmEnrollment, entity);
		mdmEnrollment.save();
		entity.setId(mdmEnrollment.getId());
		return entity;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.foreveross.bsl.system.application.DatabaseInfoApplication#updateEntity
	 * (com.foreveross.bsl.system.application.vo.DatabaseInfoVO)
	 */
	@Override
	public MdmEnrollmentVo updateEntity(String id, MdmEnrollmentVo entity) {
		MdmEnrollment MdmEnrollment = new MdmEnrollment(); // 防止可能为null
		MdmEnrollment = (MdmEnrollment) MdmEnrollment.findOne(id);
		BeanHelper.copyProperties(MdmEnrollment, entity);
		MdmEnrollment.save();
		BeanHelper.copyProperties(entity, MdmEnrollment);
		return entity;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.foreveross.bsl.system.application.DatabaseInfoApplication#deleteEntity
	 * (com.foreveross.bsl.system.application.vo.DatabaseInfoVO)
	 */
	@Override
	public void deleteEntity(String id) {
		MdmEnrollment MdmEnrollment = new MdmEnrollment();
		MdmEnrollment = (MdmEnrollment) MdmEnrollment.findOne(id);
		MdmEnrollment.delete();
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.foreveross.bsl.system.application.DatabaseInfoApplication#getEntity
	 * (java.lang.String)
	 */
	@Override
	public MdmEnrollmentVo getEntity(String id) {
		MdmEnrollment MdmEnrollment = new MdmEnrollment();
		MdmEnrollment = (MdmEnrollment) MdmEnrollment.findOne(id);
		MdmEnrollmentVo vo = new MdmEnrollmentVo();
		BeanHelper.copyProperties(vo, MdmEnrollment);
		return vo;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.foreveross.bsl.system.application.DatabaseInfoApplication#getEntity
	 * (java.lang.String)
	 */
	@Override
	public List<MdmEnrollmentVo> getEntityByTopic(String topic) {
		MdmEnrollment MdmEnrollment = new MdmEnrollment();
		OperatEnum operat = OperatEnum.EQ;
		String proName = "topic";
		List<MdmEnrollment> MdmEnrollments = (List<MdmEnrollment>) MdmEnrollment.findListByProperty(proName, operat, topic);
		if (MdmEnrollments != null) {
			List<MdmEnrollmentVo> result = new ArrayList<MdmEnrollmentVo>();
			MdmEnrollmentVo vo;
			for (Iterator iterator = MdmEnrollments.iterator(); iterator.hasNext();) {
				MdmEnrollment obj = (MdmEnrollment) iterator.next();

				vo = new MdmEnrollmentVo();
				BeanHelper.copyProperties(vo, obj);
				result.add(vo);
			}
			return result;
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.foreveross.bsl.system.application.DatabaseInfoApplication#getEntity
	 * (java.lang.String)
	 */
	@Override
	public MdmEnrollmentVo getEntityByUdid(String udid) {
		MdmEnrollment MdmEnrollment = new MdmEnrollment();
		OperatEnum operat = OperatEnum.EQ;
		List<MdmEnrollment> MdmEnrollments = (List<MdmEnrollment>)MdmEnrollment.findListByProperty("udid", operat, udid);
		if (MdmEnrollments != null && MdmEnrollments.size() > 0) {
			MdmEnrollmentVo vo = new MdmEnrollmentVo();
			BeanHelper.copyProperties(vo, MdmEnrollments.get(0));
			return vo;
		}
		return null;
	}
	
	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.foreveross.bsl.system.application.DatabaseInfoApplication#getEntity
	 * (java.lang.String)
	 */
	@Override
	public MdmEnrollmentVo getEntityByApnsToken(String apnsToken) { // 确保apnsToken是唯一
		MdmEnrollment MdmEnrollment = new MdmEnrollment();
		OperatEnum operat = OperatEnum.EQ;
		List<MdmEnrollment> MdmEnrollments = (List<MdmEnrollment>)MdmEnrollment.findListByProperty("token", operat, apnsToken);
		if (MdmEnrollments != null && MdmEnrollments.size() > 0) {
			MdmEnrollmentVo vo = new MdmEnrollmentVo();
			BeanHelper.copyProperties(vo, MdmEnrollments.get(0));
			return vo;
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.foreveross.bsl.system.application.DatabaseInfoApplication#findAll()
	 */
	@Override
	public List<MdmEnrollmentVo> findAll() {
		MdmEnrollment MdmEnrollment = new MdmEnrollment();
		Iterable iterable = MdmEnrollment.findAll();
		if (iterable != null) {
			List<MdmEnrollmentVo> result = new ArrayList<MdmEnrollmentVo>();
			MdmEnrollmentVo vo;
			for (Iterator iterator = iterable.iterator(); iterator.hasNext();) {
				MdmEnrollment obj = (MdmEnrollment) iterator.next();

				vo = new MdmEnrollmentVo();
				BeanHelper.copyProperties(vo, obj);
				result.add(vo);
			}
			return result;
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.foreveross.bsl.system.application.DatabaseInfoApplication#findByPageable
	 * (org.springframework.data.domain.Pageable)
	 */
	@Override
	public Page<MdmEnrollmentVo> findByPageable(Pageable pageable) {
		MdmEnrollment MdmEnrollment = new MdmEnrollment();
		Page page = MdmEnrollment.findAll(pageable);
		if (page != null && page.getContent() != null) {
			List<MdmEnrollmentVo> result = new ArrayList<MdmEnrollmentVo>();
			MdmEnrollmentVo vo;
			for (Iterator iterator = page.getContent().iterator(); iterator
					.hasNext();) {
				MdmEnrollment obj = (MdmEnrollment) iterator.next();

				vo = new MdmEnrollmentVo();
				BeanHelper.copyProperties(vo, obj);
				result.add(vo);
			}
			Page<MdmEnrollmentVo> pageVO = new PageImpl<MdmEnrollmentVo>(result, pageable,
					page.getTotalElements());
			return pageVO;
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.foreveross.bsl.system.application.MdmEnrollmentApplication#findByPageable
	 * (org.springframework.data.domain.Pageable,
	 * com.foreveross.bsl.system.application.vo.MdmEnrollmentVo)
	 */
	@Override
	public Page<MdmEnrollmentVo> findByPageable(Pageable pageable, MdmEnrollmentVo MdmEnrollment) {
		MdmEnrollment acc = new MdmEnrollment();
		BeanHelper.copyProperties(acc, MdmEnrollment);
		Page page = acc.findByPageable(pageable, acc);
		if (page != null && page.getContent() != null) {
			List<MdmEnrollmentVo> result = new ArrayList<MdmEnrollmentVo>();
			MdmEnrollmentVo vo;
			for (Iterator iterator = page.getContent().iterator(); iterator.hasNext();) {
				MdmEnrollment obj = (MdmEnrollment) iterator.next();

				vo = new MdmEnrollmentVo();
				BeanHelper.copyProperties(vo, obj);
				result.add(vo);
			}
			Page<MdmEnrollmentVo> pageVO = new PageImpl<MdmEnrollmentVo>(result, pageable,
					page.getTotalElements());
			return pageVO;
		}
		return null;
	}
}
